import {UIButton, UIInput, UIPanel, UIRow, UISpan, UIText} from "./libs/ui.js";
import {VertexNormalsHelper} from "./jsm/helpers/VertexNormalsHelper.js";


function PropertyGeometry(editor)
{
    var signals = editor.signals;
    var currentGeometryType = null;
    var container = new UIPanel();
    container.setBorderTop( '0' );
    container.setDisplay( 'none' );
    container.setPaddingTop( '20px' );

    // type

    var geometryTypeRow = new UIRow();
    var geometryType = new UIText();

    geometryTypeRow.add( new UIText( '类型').setWidth( '90px' ) );
    geometryTypeRow.add( geometryType );

    container.add( geometryTypeRow );

    // uuid

    var geometryUUIDRow = new UIRow();
    var geometryUUID = new UIInput().setWidth( '102px' ).setFontSize( '12px' ).setDisabled( true );
    var geometryUUIDRenew = new UIButton( '生成' ).setMarginLeft( '7px' ).onClick( function () {

        geometryUUID.setValue( THREE.MathUtils.generateUUID() );

        editor.execute( new SetGeometryValueCommand( editor, editor.selected, 'uuid', geometryUUID.getValue() ) );

    } );

    geometryUUIDRow.add( new UIText( 'uuid' ).setWidth( '90px' ) );
    geometryUUIDRow.add( geometryUUID );
    geometryUUIDRow.add( geometryUUIDRenew );

    container.add( geometryUUIDRow );

    // name

    var geometryNameRow = new UIRow();
    var geometryName = new UIInput().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {

        editor.execute( new SetGeometryValueCommand( editor, editor.selected, 'name', geometryName.getValue() ) );

    } );

    geometryNameRow.add( new UIText( '名字' ).setWidth( '90px' ) );
    geometryNameRow.add( geometryName );

    container.add( geometryNameRow );

    // parameters

    // var parameters = new UISpan();
    // container.add( parameters );

    // buffergeometry

    //container.add( new SidebarGeometryBufferGeometry( editor ) );

    // Size

    var geometryBoundingBox = new UIText().setFontSize( '12px' ).setVerticalAlign( 'middle' );

    container.add( new UIText( '包围盒' ).setWidth( '90px' ) );
    container.add( geometryBoundingBox );

    // Helpers

    var helpersRow = new UIRow().setMarginTop( '16px' ).setPaddingLeft( '90px' );
    container.add( helpersRow );

    var vertexNormalsButton = new UIButton( '显示顶点法线' );
    vertexNormalsButton.onClick( function () {

        var object = editor.selected;

        if ( editor.helpers[ object.id ] === undefined ) {
            var helper = new VertexNormalsHelper( object );
            editor.addHelper( object, helper );
        } else {
            editor.removeHelper( object );
        }

        signals.sceneGraphChanged.dispatch();

    } );
    helpersRow.add( vertexNormalsButton );

    function build() {
        var object = editor.selected;
        if ( object && object.geometry ) {
            var geometry = object.geometry;
            container.setDisplay( 'block' );
            geometryType.setValue( geometry.type );
            geometryUUID.setValue( geometry.uuid );
            geometryName.setValue( geometry.name );

            //
            // if ( currentGeometryType !== geometry.type ) {
            //     parameters.clear();
            //     if ( geometry.type === 'BufferGeometry' ) {
            //         parameters.add( new SidebarGeometryModifiers( editor, object ) );
            //     } else {
            //         var { GeometryParametersPanel } = await import( `./Sidebar.Geometry.${ geometry.type }.js` );
            //         parameters.add( new GeometryParametersPanel( editor, object ) );
            //     }
            //     currentGeometryType = geometry.type;
            // }

            if ( geometry.boundingBox === null ) geometry.computeBoundingBox();

            const boundingBox = geometry.boundingBox;
            const x = Math.floor( ( boundingBox.max.x - boundingBox.min.x ) * 1000 ) / 1000;
            const y = Math.floor( ( boundingBox.max.y - boundingBox.min.y ) * 1000 ) / 1000;
            const z = Math.floor( ( boundingBox.max.z - boundingBox.min.z ) * 1000 ) / 1000;

            geometryBoundingBox.setInnerHTML( `${x}<br/>${y}<br/>${z}` );
        } else {
            container.setDisplay( 'none' );
        }
    }

    signals.objectSelected.add(()=>{
        currentGeometryType = null;
        build();
    });

    signals.geometryChanged.add(build);

    return container;
}

export{PropertyGeometry};